package com.uglyboys.factory;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager.LayoutParams;

import com.uglyboys.R;

public class DialogFactory extends Handler implements Runnable{
	
	private static DialogFactory dialogFactory;
	private static List<OnClickListener> yesClickListenerList = new ArrayList<OnClickListener>();
	private static List<OnClickListener> noClickListenerList = new ArrayList<OnClickListener>();
	private static List<String> titleList = new ArrayList<String>();
	private static List<String> messageList = new ArrayList<String>();
	private static List<AlertDialog> dialogList = new ArrayList<AlertDialog>();
	private static List<Integer> typeList = new ArrayList<Integer>();
	public static final int ALERT = 1;
	public static final int CONFIRM = 2;
	public static final int WAITING = 3;
	
	private static int delay;
	private Activity activity;
	
	public static DialogFactory getInstance(){
		if(dialogFactory == null)
			dialogFactory = new DialogFactory();
		return dialogFactory;
	}

	private DialogFactory(){}
	
	@Override
	public synchronized void handleMessage(Message msg){
		if(!dialogList.isEmpty()){
			if(typeList.get(0) == WAITING && dialogList.get(0).isShowing()){
//				Log.d("TabletTest","IF - removendo... type = " + typeList.get(0) + ", typeList.size = " + typeList.size() + " e dialogList.size() =" + dialogList.size());
				return;
			} else{
				if(typeList.isEmpty()){
//					Log.d("TabletTest","ELSE typeList RETURN");
					return;
				}
				if(dialogList.isEmpty()){
//					Log.d("TabletTest","ELSE dialogList RETURN");
					return;
				}
			}
		} else {
			this.makeCorrectDialog(typeList.get(0));
//			for(int i = 0; i < typeList.size(); i++){
//				Log.d("TabletTest","handleMessage - typeList(" + i + ") = " + typeList.get(i));
//			}
//			for(int i = 0; i < dialogList.size(); i++){
//				Log.d("TabletTest","handleMessage - dialogList(" + i + ") = OK");
//			}
			this.show();
		}
		super.handleMessage(msg);
	}
	
	private void makeCorrectDialog(int value){
		switch (value) {
		case WAITING:
			this.makeWaitingDialog();
//			Log.i("TabletTest","ADD WAITING!");
			break;
		case ALERT:
			this.makeAlertDialog();
//			Log.i("TabletTest","ADD ALERT!");
			break;
		case CONFIRM:
			this.makeConfirmDialog();
//			Log.i("TabletTest","ADD CONFIRM!");
			break;
		default:
		}
		this.setParams();
	}
	
	private void makeAlertDialog(){
		if(dialogList.size() == titleList.size())
			titleList.add(this.activity.getString(R.string.alert_title));
		if(dialogList.size() == messageList.size())
			messageList.add(this.activity.getString(R.string.alert_msg));
		AlertDialog.Builder builder = new AlertDialog.Builder(this.activity);
		if(dialogList.size() == yesClickListenerList.size()){
			yesClickListenerList.add(new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which){}
			});
		}
		dialogList.add(builder.create());	 
	}
	
	private void makeWaitingDialog(){
		if(dialogList.size() == titleList.size())
			titleList.add(this.activity.getString(R.string.waiting_title));
		if(dialogList.size() == messageList.size())
			messageList.add(this.activity.getString(R.string.waiting_msg));
		dialogList.add(new ProgressDialog(this.activity));
	}
	
	private void makeConfirmDialog(){
		if(dialogList.size() == titleList.size())
			titleList.add(this.activity.getString(R.string.confirm_title));
		if(dialogList.size() == messageList.size())
			messageList.add(this.activity.getString(R.string.confirm_msg));
		AlertDialog.Builder builder = new AlertDialog.Builder(this.activity);
		if(dialogList.size() == yesClickListenerList.size()){
			yesClickListenerList.add(new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which){}
			});
		}
		if(dialogList.size() == noClickListenerList.size()){
			noClickListenerList.add(new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which){}
			});
		}
		dialogList.add(builder.create());	
	}
	
	private void setParams(){
		for(int i = 0; i < dialogList.size(); i++){
			dialogList.get(i).setTitle(titleList.get(i));
			dialogList.get(i).setMessage(messageList.get(i));
			dialogList.get(i).setCancelable(Boolean.FALSE);
			dialogList.get(i).setOnDismissListener(this.dismissListener);
			if(typeList.get(i) == CONFIRM){
				dialogList.get(i).setButton(DialogInterface.BUTTON_POSITIVE,
				this.activity.getString(R.string.dialog_yes),
				yesClickListenerList.get(i));
				dialogList.get(i).setButton(DialogInterface.BUTTON_NEGATIVE,
				this.activity.getString(R.string.dialog_no),
				noClickListenerList.get(i));
			} else if(typeList.get(i) == ALERT){
				dialogList.get(i).setButton(DialogInterface.BUTTON_NEUTRAL,
				this.activity.getString(R.string.dialog_ok),
				yesClickListenerList.get(i));
			}
		}
	}
	
	public Boolean isShowing(){
		if(!dialogList.isEmpty()){
			return dialogList.get(0).isShowing();
		}
		return Boolean.FALSE;
	}
	
	public synchronized void show(){
//		for(int i = 0; i < typeList.size(); i++){
//			Log.d("TabletTest","show - typeList(" + i + ") = OK");
//		}
//		for(int i = 0; i < dialogList.size(); i++){
//			Log.d("TabletTest","show - dialogList(" + i + ") = OK");
//		}
		if(!dialogList.isEmpty()){
			if(!this.isShowing()){
				dialogList.get(0).show();
				if((typeList.get(0) == WAITING) && delay > 0){
					this.postDelayed(this, delay);
					delay = 0;
				}
			}
		}
	}
	
	@Override
	public void run() {
		this.hide();
	}
	
	public void hide(){
		if(!dialogList.isEmpty()){
			if(this.isShowing()){
				dialogList.get(0).dismiss();
			}
		}
	}
	
	public void showWaitingDelayed(int delayValue){
		delay = delayValue;
//		if(!typeList.isEmpty()){
//			for(int i = 0; i < typeList.size(); i++){
//				Log.d("TabletTest","showWaitingDelayed (makeDialog) - typeList(" + i + ") = " + typeList.get(i));
//			}
//		}
		this.makeDialog(WAITING);
//		if(!typeList.isEmpty()){
//			for(int i = 0; i < typeList.size(); i++){
//				Log.d("TabletTest","showWaitingDelayed (postDelayed) - typeList(" + i + ") = " + typeList.get(i));
//			}
//		}
	}
	
	public void setAutomaticDim(Boolean value){
		if(!dialogList.isEmpty()){
			if(value)
				dialogList.get(0).getWindow().addFlags(LayoutParams.FLAG_DIM_BEHIND);
			else
				dialogList.get(0).getWindow().clearFlags(LayoutParams.FLAG_DIM_BEHIND);
		}
	}
	
	public void setActivity(Activity act){
		this.activity = act;
	}
	
	public void addTitle(String titleValue){
		titleList.add(titleValue);
	}
	
	public void setTitle(String titleValue){
		if(titleList.isEmpty())
			addTitle(titleValue);
		else
			titleList.set(titleList.size() - 1, titleValue);
	}
	
	public void addTitle(int idString){
		titleList.add(this.activity.getString(idString));
	}
	
	public void setTitle(int idString){
		if(titleList.isEmpty())
			addTitle(this.activity.getString(idString));
		else
			titleList.set(titleList.size() - 1, this.activity.getString(idString));
	}
	
	public void addMessage(String msgValue){
		messageList.add(msgValue);
	}
	
	public void setMessage(String msgValue){
		if(messageList.isEmpty())
			addMessage(msgValue);
		else 
			messageList.set(messageList.size() - 1, msgValue);
	}
	
	public void addMessage(int idString){
		messageList.add(this.activity.getString(idString));
	}
	
	public void setMessage(int idString){
		if(messageList.isEmpty())
			addMessage(this.activity.getString(idString));
		else
			messageList.set(messageList.size() - 1, this.activity.getString(idString));
	}

	public void addYesClickListener(OnClickListener listener) {
		yesClickListenerList.add(listener);
	}
	
	public void setYesClickListener(OnClickListener listener) {
		if(yesClickListenerList.isEmpty())
			addYesClickListener(listener);
		 else 
			yesClickListenerList.set(yesClickListenerList.size() - 1, listener);
	}
	
	public void addNoClickListener(OnClickListener listener) {
		noClickListenerList.add(listener);
	}
	
	public void setNoClickListener(OnClickListener listener) {
		if(noClickListenerList.isEmpty())
			addNoClickListener(listener);
		else
			noClickListenerList.set(noClickListenerList.size() - 1, listener);
	}
	
	public AlertDialog getDialog(){
		if(!dialogList.isEmpty())
			return dialogList.get(0);
		return null;
	}
	
	public synchronized void makeDialog(int value){
//		if(!typeList.isEmpty()){
//			for(int i = 0; i < typeList.size(); i++){
//				Log.d("TabletTest","makeDialog - typeList(" + i + ") = " + typeList.get(i));
//			}
//		}
		typeList.add(value);
//		if(!typeList.isEmpty()){
//			for(int i = 0; i < typeList.size(); i++){
//				Log.d("TabletTest","makeDialog (ADD) - typeList(" + i + ") = " + typeList.get(i));
//			}
//		}
		this.sendEmptyMessage(typeList.get(typeList.size() - 1));
	}
	
	private OnDismissListener dismissListener = new OnDismissListener() {
		@Override
		public void onDismiss(DialogInterface dialog) {
			if(!dialogList.isEmpty()){
				if(typeList.get(0) == CONFIRM || typeList.get(0) == ALERT){
					yesClickListenerList.remove(0);
				}
				if(typeList.get(0) == CONFIRM){
					noClickListenerList.remove(0);
				}
				dialogList.remove(0);
				typeList.remove(0);
				titleList.remove(0);
				messageList.remove(0);
			} else{
				return;
			}
//			for(int i = 0; i < typeList.size(); i++){
//				Log.d("TabletTest","onDismiss - typeList(" + i + ") = " + typeList.get(i));
//			}
//			for(int i = 0; i < dialogList.size(); i++){
//				Log.d("TabletTest","onDismiss -  dialogList(" + i + ") = OK");
//			}
			if(!typeList.isEmpty()){
//				Log.d("TabletTest","OPA typeList.get(0) = " + typeList.get(0));
				DialogFactory.this.sendEmptyMessage(typeList.get(0));
			}
		}
	};
}